package edu.drexel.cs575.taskManager.services.taskBroker;

import java.io.IOException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.util.DateTime;
import com.google.api.services.tasks.Tasks;
import com.google.api.services.tasks.Tasks.Tasklists;
import com.google.api.services.tasks.model.*;

import java.io.File;
import java.security.GeneralSecurityException;
import java.util.List;


public class broker_api {
	public static final String SCOPE = "https://www.googleapis.com/auth/tasks";
	public static final HttpTransport TRANSPORT = new NetHttpTransport();
	public static final JsonFactory JSON_FACTORY = new JacksonFactory();
	
	private String keyPath = "";
	
	public broker_api(String keyPath){
		this.keyPath = keyPath;
	}

	private GoogleCredential Connection() throws GeneralSecurityException{
		GoogleCredential credential = null;
			try {
				credential = new GoogleCredential.Builder().setTransport(TRANSPORT)
				        .setJsonFactory(JSON_FACTORY)
				        .setServiceAccountId("64106228430@developer.gserviceaccount.com")
				        .setServiceAccountScopes(SCOPE)
				        .setServiceAccountPrivateKeyFromP12File(new File(this.keyPath))
				        .build();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		  		
		return credential;
	}

	
	private Tasks create_service (GoogleCredential credential) throws IOException
		{
			HttpTransport httpTransport = new NetHttpTransport();
			JsonFactory jsonFactory = new JacksonFactory();
			Tasks service = new Tasks(httpTransport, jsonFactory, credential);
			return service;
		}
	
	private Tasks getConnection () throws GeneralSecurityException, IOException{
		Tasks service = null; 
		GoogleCredential credential =null;
		credential = Connection();
		service = create_service(credential);
		return service;
	}
	
	public String tlcreate_tasklist(String tl_name) throws IOException, GeneralSecurityException{
		
		Tasks service = null; 
		service = getConnection();
		
		TaskList taskList = new TaskList();
		taskList.setTitle(tl_name);
		TaskList result;
		result = service.tasklists().insert(taskList).execute();
		return result.getId();
	}
	
	
	public TaskLists tllist_tasklist() throws IOException, GeneralSecurityException{
		
		Tasks service = null; 
		service = getConnection();
		
		TaskLists taskLists = service.tasklists().list().execute();
		return taskLists;
	}
	
	public void tldelete_tasklist(String tl_id) throws IOException, GeneralSecurityException{
		Tasks service = null; 
		service = getConnection();
		
		if (!tl_id.contains("MTUzMjM1Njc5Mjg4MDU1NTUzMTE6MDow"))
		service.tasklists().delete(tl_id).execute();
	}
	
	public String tlget_tasklist_strname(String tl_id) throws IOException, GeneralSecurityException{
		Tasks service = null; 
		service = getConnection();
		
		TaskLists taskLists = service.tasklists().list().execute();

		for (TaskList taskList : taskLists.getItems()) {
			if (taskList.getId().equals(tl_id)){
			return taskList.getTitle();
			}
		}
		return null;
	}
	
	public boolean tlupdate_tasklist_strname(String tl_id, String tl_newname) throws IOException, GeneralSecurityException{
		Tasks service = null; 
		service = getConnection();
		
		TaskList taskList = service.tasklists().get(tl_id).execute();
		taskList.setTitle(tl_newname);
		TaskList result = service.tasklists().update(taskList.getId(), taskList).execute();
		
		if (result.getTitle() !=null){
			return true;
		}else{
			return false;
		}
					
	}
	
	public com.google.api.services.tasks.model.Tasks treturn_task_list(String tl_id) throws IOException, GeneralSecurityException{
		Tasks service = null; 
		service = getConnection();
		com.google.api.services.tasks.model.Tasks tasks = service.tasks().list(tl_id).execute();
		return tasks;
	}
	
	public String tget_task_name(String tl_id, String t_id) throws IOException, GeneralSecurityException{
		Tasks service = null; 
		service = getConnection();
		Task task = service.tasks().get(tl_id, t_id).execute();
		return task.getTitle();
	}
	
	public String tinsert_task (String tl_id, String task_nm, String task_notes, int taskdue) throws IOException, GeneralSecurityException{
		Tasks service = null; 
		service = getConnection();
		
		Task task = new Task();
		task.setTitle(task_nm);
		task.setNotes(task_notes);
		//task.setDue(new DateTime(System.currentTimeMillis() + taskdue));
		Task result = service.tasks().insert(tl_id, task).execute();
		return result.getId();
	}
	
	public void tupdate_task (String tl_id, String t_id, String updatetype, String value) throws IOException, GeneralSecurityException{
		Tasks service = null; 
		service = getConnection();
		
		Task task = service.tasks().get(tl_id, t_id).execute();
		
		if(updatetype=="status"){
			task.setStatus(updatetype);
		}
		
		Task result = service.tasks().update(tl_id, task.getId(), task).execute();

	}
	
	public void tdelete_task (String tl_id, String t_id) throws IOException, GeneralSecurityException{
		Tasks service = null; 
		service = getConnection();
		service.tasks().delete(tl_id, t_id).execute();
	}
	
	public void tclear_task_complete (String tl_id) throws IOException, GeneralSecurityException{
		Tasks service = null; 
		service = getConnection();
		
		service.tasks().clear("taskListID").execute();
	}
	
	
	public void tmove () throws IOException{
		//TBD
	}
	
	
	
	

}

